package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.pojo.PageBean;
import com.example.pojo.Privilege;
import com.example.pojo.Role;
import com.example.pojo.RolePrivilege;
import com.example.service.PrivilegeService;
import com.example.service.RolePrivilegeService;
import com.example.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 二七和夭夭
 * @email 2646498606@qq.com
 * @date 2022/8/20 17:18
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PrivilegeService privilegeService;
    @Autowired
    private RolePrivilegeService rolePrivilegeService;

    /**
     * 获取角色列表
     *
     * @return
     */
    @RequestMapping("/list.do")
    public String getRoleList(Model model, Integer currentPage, Integer pageSize) {
        if (currentPage == null) currentPage = 1;
        if (pageSize == null) pageSize = 5;
        PageBean<Role> pageBean = roleService.getPage(currentPage, pageSize);

        model.addAttribute("pageBean", pageBean);
        model.addAttribute("roleList", pageBean.getRecordList());
        model.addAttribute("url", "/role/list.do?");

        return "forward:/jsp/role/list.jsp";
    }

    /**
     * 添加角色
     *
     * @param name
     * @param description
     */
    @RequestMapping("/add.do")
    public String addRole(String name, String description) {
        //角色名为空不添加
        if (name == null || name.equals("")) {
            return "forward:list.do";
        }
        //角色添加不能重复
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getName, name);
        Role one = roleService.getOne(wrapper);

        if (one == null) {
            Role role = new Role(null, name, description);
            boolean save = roleService.save(role);

        }
        return "redirect:/role/list.do";
    }

    /**
     * 删除角色
     *
     * @return
     */
    @RequestMapping("/delete.do")
    public String deleteRole(Integer id) {
        roleService.removeRole(id);
        return "redirect:/role/list.do";
    }

    /**
     * 根据id查询角色信息(用于修改)
     *
     * @param id
     * @param req
     * @return
     */
    @RequestMapping("/editUI.do")
    public String selectById(Integer id, HttpServletRequest req) {
        Role role = roleService.getById(id);
        req.setAttribute("role", role);
        return "forward:/jsp/role/edit.jsp";
    }

    /**
     * 修改并保存角色信息
     *
     * @param
     * @param name
     * @param description
     * @return
     */
    @RequestMapping("/edit.do")
    public String updateRole(Integer rid, String name, String description) {
        Role role = new Role(rid, name, description);
        boolean update = roleService.updateById(role);

        return "redirect:/role/list.do";
    }

    /**
     * 获取更改用户权限页面
     *
     * @param rid
     * @param req
     * @return
     */
    @RequestMapping("/setPrivilegeUI.do")
    public String getPrivilege(Integer rid, HttpServletRequest req) {
        //查询当前用户信息
        Role role = roleService.getById(rid);
        req.setAttribute("role", role);

        //查询权限信息
        List<Privilege> privilegeList = privilegeService.selectRootPrivilege();
        req.setAttribute("privilegeList", privilegeList);

        //查询当前岗位已有的权限
        LambdaQueryWrapper<RolePrivilege> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePrivilege::getRid, rid);
        List<RolePrivilege> rolePrivileges = rolePrivilegeService.list(wrapper);

        List<Integer> privilegeIds = new ArrayList<>();
        for (RolePrivilege rolePrivilege : rolePrivileges) {
            privilegeIds.add(rolePrivilege.getPid());
        }
        req.setAttribute("privilegeIds", privilegeIds);

        return "forward:/jsp/role/setPrivilegeUI.jsp";
    }

    /**
     * 设置权限
     *
     * @param rid
     * @param privilegeIds
     * @return
     */
    @RequestMapping("/setPrivilege.do")
    public String setPrivilege(Integer rid, Integer[] privilegeIds) {
        //权限修改
        rolePrivilegeService.savePrivilege(rid, privilegeIds);
        return "redirect:/role/list.do";
    }

    /**
     * 转到添加页面
     *
     * @return
     */
    @RequestMapping("/addUI.do")
    public String gotoAddUI() {
        return "/jsp/role/add.jsp";
    }

}
